package com.zimmur.service.impl.designer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedMap.Builder;
import com.zimmur.common.ConstData;
import com.zimmur.dao.designer.IDesigerInfoDao;
import com.zimmur.model.desiger.DesigerInfo;
import com.zimmur.model.desiger.DesigerVoView;
import com.zimmur.model.desiger.view.DesigerInfoVo;
import com.zimmur.model.desiger.view.DesigerVo;
import com.zimmur.service.RedisCacheUtil;
import com.zimmur.service.base.IAdBannerService;
import com.zimmur.service.designer.IDesignerFavService;
import com.zimmur.service.designer.IDesignerInfoService;
import com.zimmur.service.zone.ITopicFavService;


@Service("designerInfoService")
public class DesignerInfoServiceImpl implements IDesignerInfoService {
	private static Logger log=LoggerFactory.getLogger(DesignerInfoServiceImpl.class);
	
	@Resource
	private IDesigerInfoDao designerDao;
	
	@Resource
	private IDesignerFavService designerFavService;
	
	@Resource
	private IAdBannerService abBannerService;
	@Resource
	private ITopicFavService topicFavService;
	
	@Resource
	private RedisCacheUtil<DesigerVoView> stringRedisDao;
/*	@Resource
	private RedisCacheUtil<ImmutableSortedMap<String,List<DesigerVo>>> stringRedisDao;
*/	
	private final static String REDIS_KEY ="designerInfoService";
	
	private static Object syncLock = new Object();

	
	@Override
	public DesigerInfo selectByUserId(long userId) {
		return designerDao.selectByUserIdDesigerInfo(userId);
	}

	@Override
	public List<DesigerInfo> selectByTags(String tags,int pageNow,int pageSize) {
		if(pageNow>0){
			pageNow = (pageNow -1)*pageSize;
		}
		return designerDao.selectByDesigerTag(tags,pageNow,pageSize);
	}

	@Override
	public int selectByTagsCount(String tags) {
		return designerDao.selectByDesigerTagCount(tags);
	}

	@Override
	public int selectByTagsAndNameCount(String key) {
		return designerDao.selectCountByLikeNickNameAndTagsCount(key);
	}

	@Override
	public List<DesigerInfo> selectByTagsAndNameList(String key, int pageNow, int pageSize) {
		if(pageNow == 0)
			pageNow = 1;
		int beginNo = (pageNow-1)*pageSize;
		return designerDao.selectCountByLikeNickNameAndTags(key, beginNo, pageSize);
	}

	@Override
	public List<DesigerVo> selectDesiger(String key,String tags, int pageNow, int pageSize, Long createUserId) {
		return designerDao.selectDesiger(key,tags, (pageNow-1)*pageSize, pageSize, createUserId);
	}

	@Override
	public int countDesiger(String key,String tags) {
		return designerDao.countDesiger(key,tags);
	}

	@Override
	public DesigerInfoVo selectDesigerByUserId(long userId,long createUserId) {
		return designerDao.selectDesigerByUserId(userId,createUserId);
	}

	
	@Override
	public List<DesigerVoView> selectAll() {
		List<DesigerVoView> desigerList=null;
		String key = REDIS_KEY  + "_designerList_selectAll";
		try {
			//是否已存在
			boolean bRedis = false;
			try {
				bRedis = stringRedisDao.exists(key);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(bRedis){
				//从缓存中读取
				desigerList = stringRedisDao.getCacheList(key);
			}else{
				synchronized (syncLock) {
					try {
						bRedis = stringRedisDao.exists(key);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(bRedis){
						//从缓存中读取
						desigerList = stringRedisDao.getCacheList(key);
					}else{
						//缓存中不存在，创建后存入
						//从数据库读取
						//prefix字段为字母
						List<DesigerVo>	 desigerVos = designerDao.selectAll(1);
						//prefix字段不是字母，如符号，null,空字符串
						List<DesigerVo> desigerVos1=designerDao.selectAll(2);
						desigerList=divideDesiger(desigerVos,desigerVos1);
						try {
							//存入缓存
							stringRedisDao.setCacheList(key, desigerList);
							stringRedisDao.setTimeout(key, ConstData.REDIS_KEY_TIMEOUT);
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return desigerList;
	}
	

	private List<DesigerVoView> divideDesiger(List<DesigerVo> desigerVos,List<DesigerVo> otherDesiger){
		List<DesigerVoView> desigerVoViewList=new ArrayList<>();
		List<DesigerVo> temp=new ArrayList<>();
		if (desigerVos!=null && desigerVos.size()>0) {
			String prefix=desigerVos.get(0).getPrefix();
			Iterator<DesigerVo> iterator= desigerVos.iterator();
			while (iterator.hasNext()){
				DesigerVo vo = iterator.next();
				String prefix_i=vo.getPrefix();
				if(prefix.equals(prefix_i)){
					temp.add(vo);
				}else{
					desigerVoViewList.add(new DesigerVoView(prefix,temp));
					prefix=prefix_i;
					temp=new ArrayList<>();
					temp.add(vo);
				}
			}
			desigerVoViewList.add(new DesigerVoView(prefix,temp));
		}
		if(otherDesiger!=null && otherDesiger.size()>0){
			desigerVoViewList.add(new DesigerVoView("#",otherDesiger));
		}
		return desigerVoViewList;
	}
	/*private ImmutableSortedMap<String,List<DesigerVo>> divideDesingerMap(List<DesigerVo> desigerVos,List<DesigerVo> otherDesiger){
		Builder<String,  List<DesigerVo>> maps = ImmutableSortedMap.naturalOrder();

		//Map<String, List<DesigerVo>> maps=new HashMap<>();
		List<DesigerVo> temp=new ArrayList<>();
		if (desigerVos!=null && desigerVos.size()>0) {
			String prefix=desigerVos.get(0).getPrefix();
			Iterator<DesigerVo> iterator= desigerVos.iterator();
			while (iterator.hasNext()){
				DesigerVo vo = iterator.next();
				String prefix_i=vo.getPrefix();
				if(prefix.equals(prefix_i)){
					temp.add(vo);
				}else{
					maps.put(prefix, temp);
					temp=new ArrayList<>();
					temp.add(vo);
					prefix=prefix_i;
				}
			}
			maps.put(prefix, temp);
		}
		if(otherDesiger!=null && otherDesiger.size()>0){
			maps.put("~", otherDesiger);
		}
		
		ImmutableSortedMap<String, List<DesigerVo>> orderlyMap = maps.build();

		return orderlyMap;
	}*/
}
